import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:x_common_utils/utils/inkWell_util.dart';
import 'package:xinxuan/common/bank_agreement.dart';
import 'package:xinxuan/common/button_util.dart';
import 'package:xinxuan/common/input_item.dart';
import 'package:xinxuan/common/input_item_sms_code.dart';
import 'package:xinxuan/common/scroll_configuration_common.dart';
import 'package:xinxuan/controllers/common_page_controller.dart';
import 'package:xinxuan/controllers/my_controller/pay_bank_controller/add_pay_bank_page_controller.dart';
import 'package:xinxuan/utils/color_util.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class AddPayBank extends StatelessWidget {

  final CommonPageController commonPageController = Get.find();

  @override
  Widget build(BuildContext context) {
    return GetBuilder<AddPayBankPageController>(
      init: AddPayBankPageController(),
      builder: (controller) {
        return Column(
          children: [
            Expanded(child: ScrollConfigurationCommon(child: ListView(
              children: [
                InputItem(
                  title: '开户名称', 
                  hintText: '请填写开户名称',
                  maxLength: 10,
                  onChenge: (String value) {
                    controller.payerName = value;
                  }, 
                ),
                InputItem(
                  title: '卡号', 
                  hintText: '请填写银行卡号',
                  inputFormatters: [
                    FilteringTextInputFormatter.allow(RegExp('[0-9]')),
                  ],
                  keyBoardType: TextInputType.number,
                  maxLength: 20,
                  onChenge: (String value) {
                    controller.cardNumber = value;
                  }, 
                ),
                Obx(() => _bankListItem(controller)),
                Obx(() => _authenticationType(controller)),
                InputItem(
                  title: '认证信息', 
                  hintText: '请填写对应的认证信息',
                  maxLength: 20,
                  keyBoardType: TextInputType.number,
                  onChenge: (String value) {
                    controller.identityNumber = value;
                  }, 
                ),
                InputItem(
                  title: '开户手机', 
                  maxLength: 11,
                  hintText: '请填写开户对应的手机号',
                  inputFormatters: [
                    FilteringTextInputFormatter.allow(RegExp('[0-9]')),
                  ],
                  keyBoardType: TextInputType.number,
                  onChenge: (String value) {
                    controller.mobile = value;
                  }, 
                ),
                Obx(() => InputItemSmsCode(
                  duration: controller.duration.value,
                  value: controller.code,
                  defaultValue: controller.code,
                  showBorder: false,
                  onChenge: (String value) {
                    controller.code = value;
                  }, 
                  onTap: () {
                    controller.getSmsCode();
                  }
                )),
                commonPageController.userData.value.isPayPassword == 0 ? Column(
                  children: [
                    SizedBox(height: 12.h),
                    InputItem(
                      title: '支付密码', 
                      hintText: '支付密码必须6位数字',
                      obscureText: true,
                      maxLength: 6,
                      keyBoardType: TextInputType.visiblePassword,
                      inputFormatters: [
                        FilteringTextInputFormatter.allow(RegExp('[0-9]')),
                      ],
                      onChenge: (String value) {
                        controller.password = value;
                      }, 
                    ),
                    InputItem(
                      title: '确认密码', 
                      hintText: '请确认支付密码',
                      obscureText: true,
                      maxLength: 6,
                      keyBoardType: TextInputType.visiblePassword,
                      inputFormatters: [
                        FilteringTextInputFormatter.allow(RegExp('[0-9]')),
                      ],
                      onChenge: (String value) {
                        controller.confirmPassword = value;
                      }, 
                    ),
                  ],
                ) : Container()
              ],
            ))),
            Container(
              height: 92.h + ScreenUtil().bottomBarHeight,
              padding: EdgeInsets.only(bottom: ScreenUtil().bottomBarHeight),
              color: bgWhiteColor,
              child: Column(
                children: [
                  Obx(() => BankAgreement(
                    consentAgreement: controller.consentAgreement.value, 
                    onTap: () {
                      controller.consentAgreement.value = !controller.consentAgreement.value;
                    }
                  )),
                  Container(
                    height: 56.h,
                    padding: EdgeInsets.symmetric(horizontal: 12.w),
                    alignment: Alignment.center,
                    child: ButtonUtil(
                      text: '确定', 
                      fontSize: 14.sp, 
                      borderRadius: 30.w, 
                      height: 36.h, 
                      onTop: () {
                        controller.determineAddPayBank();
                      }
                    ),
                  )
                ],
              ),
            )
          ],
        );
      },
    );
  }

  /// 银行卡下拉项
  Widget _bankListItem(AddPayBankPageController controller) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 12.w),
      color: bgWhiteColor,
      child: Container(
        height: 54.h,
        padding: EdgeInsets.symmetric(horizontal: 4.w),
        decoration: BoxDecoration(
          border: Border(
            bottom: BorderSide(
              width: .5,
              color: lineColor
            )
          )
        ),
        child: Row(
          children: [
            Container(
              width: 84.w,
              child: Text(
                '所属银行',
                style: TextStyle(
                  color: textBlackColor,
                  fontSize: 16.sp
                ),
              ),
            ),
            Expanded(
              child: InkWellUtil.shakeProofInkWell(Row(
                children: [
                  Expanded(child: Container(
                    child: Text(
                      controller.selectedBank.value.id == 0 ? '所属银行' : '${controller.selectedBank.value.title}',
                      style: TextStyle(
                        color: textBlackColor,
                        fontSize: 16.sp
                      ),
                    ),
                  )),
                  Image.asset(
                    'assets/icons/icon_arrow_right.png',
                    width: 15.w,
                    height: 15.w,
                  )
                ],
              ), () {
                Get.bottomSheet(Container(
                  height: 300.h,
                  decoration: BoxDecoration(
                    color: bgWhiteColor,
                    borderRadius: BorderRadius.vertical(top: Radius.circular(5.w))
                  ),
                  child: Column(
                    children: [
                      Container(
                        height: 54.h,
                        padding: EdgeInsets.symmetric(horizontal: 16.w),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            InkWellUtil.shakeProofInkWell(Text(
                              '取消',
                              style: TextStyle(
                                color: textSubtitleColor,
                                fontSize: 16.sp
                              ),
                            ), () {
                              Get.back();
                            }),
                            InkWellUtil.shakeProofInkWell(Text(
                              '确认',
                              style: TextStyle(
                                color: mainTextColor,
                                fontSize: 16.sp
                              ),
                            ), () {
                              controller.determineBank();
                              Get.back();
                            })
                          ],
                        ),
                      ),
                      Expanded(
                        child: ScrollConfigurationCommon(child: CupertinoPicker(
                          scrollController: controller.scrollController.value,
                          diameterRatio: 5.w,
                          itemExtent: 54.h, 
                          onSelectedItemChanged: (int index) {
                            controller.changeBankIndex(index);
                          }, 
                          children: controller.systemBankArr.map((item) {
                            return Container(
                              height: 54.h,
                              alignment: Alignment.center,
                              child: Text(
                                '${item.title}',
                                style: TextStyle(
                                  color: textBlackColor,
                                  fontSize: 16.sp
                                ),
                              ),
                            );
                          }).toList()
                        )),
                      )
                    ],
                  ),
                ));
              }),
            )
          ],
        ),
      ),
    );
  }

  Widget _authenticationType(AddPayBankPageController controller) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 12.w),
      color: bgWhiteColor,
      child: Container(
        height: 54.h,
        padding: EdgeInsets.symmetric(horizontal: 4.w),
        decoration: BoxDecoration(
          border: Border(
            bottom: BorderSide(
              width: .5,
              color: lineColor
            )
          )
        ),
        child: Row(
          children: [
            Container(
              width: 84.w,
              child: Text(
                '认证类型',
                style: TextStyle(
                  color: textBlackColor,
                  fontSize: 16.sp
                ),
              ),
            ),
            Expanded(
              child: InkWellUtil.shakeProofInkWell(Row(
                children: [
                  Expanded(child: Container(
                    child: Text(
                      controller.selectedIdentityType.value.id == 0 ? '请选择认证类型' : '${controller.selectedIdentityType.value.title}',
                      style: TextStyle(
                        color: textBlackColor,
                        fontSize: 16.sp
                      ),
                    ),
                  )),
                  Image.asset(
                    'assets/icons/icon_arrow_right.png',
                    width: 15.w,
                    height: 15.w,
                  )
                ],
              ), () {
                Get.bottomSheet(Container(
                  height: 300.h,
                  decoration: BoxDecoration(
                    color: bgWhiteColor,
                    borderRadius: BorderRadius.vertical(top: Radius.circular(5.w))
                  ),
                  child: Column(
                    children: [
                      Container(
                        height: 54.h,
                        padding: EdgeInsets.symmetric(horizontal: 16.w),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            InkWellUtil.shakeProofInkWell(Text(
                              '取消',
                              style: TextStyle(
                                color: textSubtitleColor,
                                fontSize: 16.sp
                              ),
                            ), () {
                              Get.back();
                            }),
                            InkWellUtil.shakeProofInkWell(Text(
                              '确认',
                              style: TextStyle(
                                color: mainTextColor,
                                fontSize: 16.sp
                              ),
                            ), () {
                              controller.determineIdentityType();
                              Get.back();
                            })
                          ],
                        ),
                      ),
                      Expanded(
                        child: ScrollConfigurationCommon(child: CupertinoPicker(
                          scrollController: controller.authenticationTypeScrollController.value,
                          diameterRatio: 5.w,
                          itemExtent: 54.h, 
                          onSelectedItemChanged: (int index) {
                            controller.changeIdentityTypeIndex(index);
                          }, 
                          children: controller.authenticationTypeArr.map((item) {
                            return Container(
                              height: 54.h,
                              alignment: Alignment.center,
                              child: Text(
                                '${item.title}',
                                style: TextStyle(
                                  color: textBlackColor,
                                  fontSize: 16.sp
                                ),
                              ),
                            );
                          }).toList()
                        )),
                      )
                    ],
                  ),
                ));
              }),
            )
          ],
        ),
      ),
    );
  }
}